//秒杀的javascript
var seckill = {
    URL: {
        now: function () {
            return "/seckill/time/now";
        }, exposer: function (seckillId) {
            return "/seckill/" + seckillId + "/exposer";
        }, execution: function (seckillId, md5) {
            return "/seckill/" + seckillId + "/" + md5 + "/execution";
        }
    },
    handlerSeckill: function (seckillId) {
        var seckillbox = $("#seckill-box");
        seckillbox.hide().html("<button class='btn btn-primary' id='killBtn'>开始秒杀</button>");
        $.post(seckill.URL.exposer(seckillId)).done(function (res) {
            if (res && res.success) {
                var exposer = res.data;
                var md5 = exposer.md5;
                if (exposer.exposed) {
                    var killUrl = seckill.URL.execution(seckillId, md5);

                    $("#killBtn").one('click', function () {
                        //绑定执行秒杀请求的操作
                        $(this).addClass('disabled');
                        $.post(killUrl, {}).done(function (res) {
                            if (res && res.success) {
                                var killResult = res.data;
                                var state = killResult.state;
                                var stateInfo = killResult.stateInfo;
                                seckillbox.html("<span class='label label-success'>" + stateInfo + "</span>");
                            }
                        })
                    });

                    seckillbox.show();

                } else {
                    var now = exposer.now;
                    var start = exposer.start;
                    var end = exposer.end;
                    seckill.countdown(seckillId, now, start, end);
                }
            }
        })
    },
    //验证手机号
    validatePhone: function (phone) {
        if (phone && phone.length == 11) {
            return true;
        } else {
            return false;
        }
    },
    countdown: function (seckillId, nowTime, startTime, endTime) {

        console.log(nowTime)
        console.log(endTime)

        console.log(nowTime > endTime);

        if (nowTime > endTime) {
            $("#seckill-box").html("<span class='text-danger'>秒杀已结束</span>")
        } else if (nowTime < startTime) {
            //秒杀未开始
            var killTime = new Date(startTime);
            $("#seckill-box").countdown(killTime, function (event) {
                var format = event.strftime("倒计时：%D天 %H时 %M分 %S秒");
                $("#seckill-box").html("<h3>秒杀未开始：<span class='text-danger'>" + format + "</span></h3>");
            }).on('finish.countdown', function () {
                seckill.handlerSeckill(seckillId);
            });
        } else {
            seckill.handlerSeckill(seckillId);
        }
    },
    detail: {

        init: function (params) {
            //手机验证和登录，计时交互
            console.log(params);
            var killPhone = $.cookie("killPhone");

            if (!seckill.validatePhone(killPhone)) {
                //绑定手机号
                var bindPhoneModal = $("#bindPhoneModal");
                bindPhoneModal.modal({
                    show: true,
                    backdrop: "static",
                    keyboard: false
                });
                $("#killPhoneBtn").click(function () {
                    var inputPhone = $("#input-phone").val();
                    if (seckill.validatePhone(inputPhone)) {
                        $.cookie("killPhone", inputPhone, {exipres: 7, path: "/seckill"});
                        window.location.reload();
                    } else {
                        $("#killPhoneMsg").hide().html("<label class='label label-danger'>手机号错误！</label>").show(300);
                    }
                })
                return;
            }
            //已经登录
            //计时交互
            var seckillId = params['seckillId'];
            var startTime = params['startTime'];
            var endTime = params['endTime'];
            $.get(seckill.URL.now()).done(function (res) {
                if (res && res.success) {
                    var nowTime = res.data;
                    seckill.countdown(seckillId, nowTime, startTime, endTime);
                }
            })
        }
    }
}